package com.kyzh.core.dialog

import android.app.Activity
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.tabs.TabLayoutMediator
import com.google.common.reflect.TypeToken
import com.google.gson.Gson
import com.gushenge.core.beans.GiftUserInfo
import com.gushenge.core.beans.RoomGift
import com.gushenge.core.beans.RoomGiftList
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.requests.LiveRequest
import com.kyzh.core.R
import com.kyzh.core.activities.BrowserActivity
import com.kyzh.core.databinding.TrtcvoiceroomDialogSendGiftBinding
import com.kyzh.core.databinding.TrtcvoiceroomDialogTopUserItemBinding
import com.kyzh.core.pager.weal.live.SendGiftItemFragment
import com.kyzh.core.utils.ViewUtils.dp
import com.kyzh.core.utils.startActivity
import com.gushenge.core.toast
import com.kyzh.core.utils.setVisibility
import kotlinx.android.synthetic.main.activity_about.*
import per.goweii.layer.core.ktx.onClickToDismiss
import per.goweii.layer.core.widget.SwipeLayout
import per.goweii.layer.dialog.DialogLayer
import per.goweii.layer.dialog.ktx.backgroundDimDefault
import per.goweii.layer.dialog.ktx.gravity
import per.goweii.layer.dialog.ktx.swipeDismiss

private var dialog : DialogLayer?=null
private var selectUserCount = 0
fun Activity.showSendGiftDialog(roomId:String, users:ArrayList<GiftUserInfo>, listener: (users:ArrayList<GiftUserInfo>, gift:RoomGift)->Unit){
    dialog = DialogLayer(this)
    dialog!!.setContentView(getView(roomId,users,listener))
        .backgroundDimDefault()
        .gravity(Gravity.BOTTOM)
        .swipeDismiss(SwipeLayout.Direction.BOTTOM)
        .onClickToDismiss(android.R.id.text1)
        .show()
}


private fun Activity.getView(roomId:String, users:ArrayList<GiftUserInfo>, listener: (users:ArrayList<GiftUserInfo>, gift: RoomGift)->Unit) = TrtcvoiceroomDialogSendGiftBinding.inflate(this.layoutInflater).apply {
    selectUserCount = 0
    val userInfoAdapter = UserInfoAdapter(users)
    rvUsers.apply {
        layoutManager = LinearLayoutManager(this@getView,RecyclerView.HORIZONTAL,false)
        adapter = userInfoAdapter
    }
    btCheckAll.setOnClickListener {
        users?.forEach {
            it.isCheck = true
            selectUserCount =2
        }
        userInfoAdapter.notifyDataSetChanged()
    }
    LiveRequest.roomGift(roomId){
        val selectGifts = arrayListOf<RoomGift>()
        this.forEach {
            selectGifts.add(RoomGift())
        }
        MMKVConsts.selectGiftList  = Gson().toJson(selectGifts)
        val adapter = ViewPagerAdapter(this@getView as FragmentActivity,this)
        viewPager.adapter = adapter
        TabLayoutMediator(tabLayout, viewPager){ tab, position->
            tab.text = this[position].title
        }.apply {
            attach()
        }
        tvSend.setOnClickListener {
            val selectGift = Gson().fromJson<ArrayList<RoomGift>>(
                    MMKVConsts.selectGiftList,
                    object: TypeToken<ArrayList<RoomGift>>() {
                    }.type
                )[viewPager.currentItem]
                if (selectGift.id == 0){
                    "请选择礼物".toast()
                }else{
                    LiveRequest.roomGiftSend(this[viewPager.currentItem].type,selectGift.id,users[0].userId,roomId,tvCount.text.toString().toInt()){
                        Log.e("getView: ",selectGift.toString() )
                        listener(users!!,selectGift)
                        dialog?.dismiss()
                    }
                }
        }
    }
    LiveRequest.yuE {
        tvZuanshi.text = coin.toString()
        tvCharge.setOnClickListener {
            startActivity<BrowserActivity>(GlobalKeys.TITLE to "钻石充值", GlobalKeys.LINK to  url)
        }
    }
    tvCount.setOnClickListener {
        if (rvCount.visibility == View.GONE){
            rvCount.setVisibility(true)
        }else{
            rvCount.setVisibility(false)
        }
    }
    val list = arrayListOf<Int>()
    for (i in 1 until 100){
        list.add(i)
    }
    rvCount.adapter = object :BaseQuickAdapter<Int, BaseViewHolder>(android.R.layout.simple_list_item_1,list){
        override fun convert(holder: BaseViewHolder, item: Int) {
            val text = holder.getView<TextView>(android.R.id.text1)
            text.gravity = Gravity.CENTER
            text.text = item.toString()
            text.height = 40.dp
            text.setOnClickListener {
                tvCount.text = item.toString()
                rvCount.setVisibility(false)
            }
        }

    }
}.root


private class UserInfoAdapter(users: ArrayList<GiftUserInfo>?): BaseQuickAdapter<GiftUserInfo,BaseDataBindingHolder<TrtcvoiceroomDialogTopUserItemBinding>>(
    R.layout.trtcvoiceroom_dialog_top_user_item,users){
    override fun convert(
        holder: BaseDataBindingHolder<TrtcvoiceroomDialogTopUserItemBinding>,
        item: GiftUserInfo
    ) {
        holder.dataBinding?.apply {
            data = item
            ivHead.borderWidth = if (item.isCheck) 2.dp else 0
        }
        holder.itemView.setOnClickListener {
            item.isCheck = !item.isCheck
            if (item.isCheck){
                selectUserCount +=1
            }else{
                if (selectUserCount !=0){
                    selectUserCount -=1
                }
            }
            notifyItemChanged(holder.layoutPosition)
        }
    }
}
private class ViewPagerAdapter(context: FragmentActivity,val  beans:ArrayList<RoomGiftList>): FragmentStateAdapter(context){
    override fun getItemCount(): Int {
        return beans.size
    }


    override fun createFragment(position: Int): Fragment {
        return SendGiftItemFragment.newInstance(beans[position].list,position)
    }

}

